#include "../../include/http/HttpResponse.h"
#include "../../include/http/HttpRequest.h"
namespace http
{
    HttpResponse::HttpResponse(bool close)
        : _status_code(Unknown),
          _close_connection(close)
    {
    }

    // 将响应报文添加到缓冲区
    void HttpResponse::appendToBuffer(muduo::net::Buffer *outputBuf) const
    {
        char buf[32]; // 响应行
        // 设置响应行
        snprintf(buf, sizeof(buf), "%s %d ", _version.c_str(), _status_code);
        outputBuf->append(buf);
        outputBuf->append(_status_message);
        outputBuf->append("\r\n");

        if (_close_connection) // 如果连接关闭，则添加Connection: close头
        {
            outputBuf->append("Connection: Close\r\n");
        }
        else // 如果连接不关闭，则添加Connection: keep-alive头
        {
            outputBuf->append("Connection: Keep-Alive\r\n");
        }
        // 响应头
        for (const auto &header : _headers)
        {
            outputBuf->append(header.first);
            outputBuf->append(": ");
            outputBuf->append(header.second);
            outputBuf->append("\r\n");
        }
        // 响应体
        outputBuf->append("\r\n");
        outputBuf->append(_content);
    }
    void HttpResponse::setStatusLine(const std::string &version, HttpStatus status_code, const std::string &status_message)
    {
        _version = version;               // 设置HTTP版本
        _status_code = status_code;       // 设置状态码
        _status_message = status_message; // 设置状态消息
    }
}